<!-- 编辑弹窗 -->
<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
    :lock-scroll="false"
    :title="isUpdate ? '修改平台用户' : '添加平台用户'"
    :visible="visible"
    custom-class="ele-dialog-form"
    width="560px"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-form-item label="企业ID:" prop="companyId">
        <el-col :span="16" style="width: 316px">
          <el-input
            v-model="form.companyId"
            :maxlength="20"
            clearable
            :disabled="isUpdate"
            placeholder="请输入企业ID"
          />
        </el-col>
        <el-col :span="4" style="margin-left: 10px">
          <el-button type="primary" plain @click="generate" :disabled="isUpdate"
            >生成企业ID</el-button
          >
        </el-col>
      </el-form-item>

      <el-form-item label="企业账户:" prop="username">
        <el-input
          :disabled="isUpdate"
          v-model="form.username"
          :maxlength="20"
          clearable
          placeholder="请输入企业账户"
        />
      </el-form-item>
      <el-form-item label="企业密码:" prop="password">
        <el-input
          v-model="form.password"
          :maxlength="20"
          clearable
          :placeholder="[isUpdate ? '请输入企业密码, 不修改密码请留空' : '请输入企业密码']"
        />
      </el-form-item>
      <el-form-item label="企业名称:">
        <el-input
          v-model="form.company"
          clearable
          placeholder="请输入企业名称"
        />
      </el-form-item>
      <el-form-item label="负责人:">
        <el-input
          v-model="form.nickname"
          :maxlength="20"
          clearable
          placeholder="请输入企业负责人"
        />
      </el-form-item>
      <el-form-item label="到期时间:">
        <el-date-picker
          v-model="form.expires"
          type="datetime"
          style="width: 438px"
          placeholder="选择到期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="子用户数:">
        <el-input-number
          v-model="form.usersnum"
          :max="9999"
          :min="1"
          class="ele-fluid ele-text-left"
          placeholder="请输入子用户数,留空或0不限制子用户数."
          controls-position="right"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="授权域名:">
        <el-input
          v-model="form.domainname"
          :maxlength="200"
          :rows="4"
          placeholder='请输入授权域名, 多个域名请用","号分开, 留空不绑定域名.'
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消 </el-button>
      <el-button :loading="loading" type="primary" @click="save"
        >确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "SystemUseradminEdit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, { companyId: "" }, this.data),
      // 表单验证规则
      rules: {
        companyId: [
          {
            required: true,
            type: "string",
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error("请输入企业ID"));
              }
              this.$http
                .get("/useradmin/query?companyId=" + value)
                .then((res) => {
                  if (res.data.code !== 0 || !res.data.data.length) {
                    return callback();
                  }
                  if (
                    this.isUpdate &&
                    res.data.data[0].companyId === this.data.companyId
                  ) {
                    return callback();
                  }
                  callback(new Error("企业ID已经存在"));
                })
                .catch(() => {
                  callback();
                });
            },
          },
        ],
        username: [
          {
            required: true,
            type: "string",
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error("请输入用户名"));
              }
              this.$http
                .get("/useradmin/query?username=" + value)
                .then((res) => {
                  if (res.data.code !== 0 || !res.data.data.length) {
                    return callback();
                  }
                  if (
                    this.isUpdate &&
                    res.data.data[0].username === this.data.username
                  ) {
                    return callback();
                  }
                  callback(new Error("用户名已经存在"));
                })
                .catch(() => {
                  callback();
                });
            },
          },
        ],
        password: [
          {
            required: true,
            type: "string",
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (!this.isUpdate && !value) {
                return callback(new Error("请输入密码"));
              }
              callback();
            },
          },
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (Object.keys(this.data).length > 0) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        for (let i in this.form) {
          this.form[i] = null;
        }
        this.isUpdate = false;
      }
    },
  },
  methods: {
    generate() {
      let companyId = "1";
      for (let i = 0; i < 7; i++) {
        companyId += Math.floor(Math.random() * 10);
      }
      this.form.companyId = companyId + "";
    },

    /* 确定编辑 */
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http
            .post(
              "/useradmin/" + [this.isUpdate ? "update" : "create"],
              this.form
            )
            .then((res) => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message({ type: "success", message: res.data.msg });
                if (!this.isUpdate) {
                  for (let i in this.form) {
                    this.form[i] = null;
                  }
                }
                this.updateVisible(false);
                this.$emit("done", this.isUpdate);
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    },
  },
};
</script>
